package com.sgb.goods.utils.helper;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.IdRes;
import androidx.databinding.BindingAdapter;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.bumptech.glide.Glide;
import com.sgb.goods.R;
import com.sgb.goods.utils.base.GoodsSdk;
import com.sgb.goods.utils.base.adapter.BaseCommonAdapter;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.util.DisplayUtil;

/**
 * 公用绑定helper
 * @author cyj
 */
public class BindHelper {

    /**
     * recyclerView adapter绑定，
     * 默认绑定使用 {@link com.swgk.core.recyclerview.adapter.ItemViewHolder} 实现ViewBinding
     * <p><pre><code>
     *     <androidx.recyclerview.widget.RecyclerView
     *          android:id="@+id/rlv_view"
     *          android:layout_width="match_parent"
     *          android:layout_height="match_parent"
     *          goods_bindAdapter2="@{viewModel.orderListAdapter}"
     *          app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
     * </code></pre>
     * 简便的统一在xml管理绑定；
     * 你可以不使用此类绑定并使用传统代码方式在activity中进行setAdapter。
     * </p>
     * @param recyclerView    RecyclerView
     * @param adapter         adapter<ItemViewHolder>
     */
    @BindingAdapter("goods_bindAdapter2")
    public static void recycleViewBindAdapter1(RecyclerView recyclerView, BaseCommonAdapter adapter) {
        adapter.bindToRecyclerView(recyclerView);
    }

    @BindingAdapter("goods_bindAdapter")
    public static void recycleViewBindAdapter2(RecyclerView recyclerView, BaseCommonAdapter adapter) {
        adapter.setEmptyView(R.layout.goods_view_no_data, recyclerView);
        adapter.getEmptyView().setVisibility(View.GONE);
        adapter.bindToRecyclerView(recyclerView);
    }

    /**
     * RecyclerView添加1dp的分割线
     * @param recyclerView    RecyclerView
     * @param dpSize    分割线高度，单位dp
     * @param colorId    分割线颜色，color id
     */
    @BindingAdapter(value = {"goods_decoration_dp_size", "goods_decoration_color_id"})
    public static void recycleViewAddItemDecoration(RecyclerView recyclerView, float dpSize, @IdRes int colorId) {
        if(null == recyclerView) {
            return;
        }
        if(0 >= dpSize) {
            return;
        }
        recyclerView.addItemDecoration(new RecyclerCustomDecoration(0,
                0,
                0,
                DisplayUtil.dip2px(GoodsSdk.getApplication(), dpSize), colorId));
    }

    /**
     * ViewPager2 adapter绑定，
     * 默认绑定使用 {@link com.swgk.core.recyclerview.adapter.ItemViewHolder} 实现ViewBinding
     * <p><pre><code>
     *     <androidx.viewpager2.widget.ViewPager2
     *          android:id="@+id/vp_view"
     *          android:layout_width="match_parent"
     *          android:layout_height="match_parent"
     *          bindAdapter="@{viewModel.orderListAdapter}" />
     * </code></pre>
     * 简便的统一在xml管理绑定；
     * 你可以不使用此类绑定并使用传统代码方式在activity中进行setAdapter。
     * </p>
     * @param viewPager2    ViewPager2
     * @param adapter         adapter<ItemViewHolder>
     */
    @BindingAdapter("bindAdapter")
    public static void viewPager2BindAdapter1(ViewPager2 viewPager2, RecyclerView.Adapter adapter) {
        viewPager2.setAdapter(adapter);
    }

    /**
     * ViewPager2 adapter绑定，
     * {@link #viewPager2BindAdapter1(ViewPager2, RecyclerView.Adapter)}
     * @param viewPager2 ViewPager2
     * @param adapter    FragmentStateAdapter
     */
    @BindingAdapter("bindAdapter")
    public static void viewPager2BindAdapter2(ViewPager2 viewPager2, FragmentStateAdapter adapter) {
        viewPager2.setAdapter(adapter);
    }

    /**
     * 商品图片绑定统一处理加载网络图
     * @param view
     * @param imageUrl
     */
    @BindingAdapter("goods_imageUrl")
    public static void bindGoodsImageUrl(ImageView view, String imageUrl) {
        Glide.with(view.getContext()).load(imageUrl)
                .placeholder(R.mipmap.goods_bg_null_img_list)
                .error(R.mipmap.goods_bg_null_img_list).into(view);
    }

    /**
     * 空字符串占位符
     * {@link #binTextData(TextView text,String setText)}
     * @param text
     * @param setText 字符串内容
     */
    @BindingAdapter("setText")
    public static void binTextData(TextView text,String setText){
        if(!TextUtils.isEmpty(setText)){
            text.setText(setText);
        }else{
            text.setText("/");
        }
    }
}
